package com.example.demo.controller;


import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.example.demo.common.utils.AlipayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AlipayController {


    /**
     * 调用支付宝接口
     */
    @RequestMapping(value = "/goAlipay", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String goAlipay(String orderId, String account) throws Exception {


        //获得初始化的AlipayClient
        AlipayClient alipayClient = (AlipayClient) new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //设置请求参数


        System.out.println("付款：");
        System.out.println("orderId:"+orderId+";"+"account:"+account);
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderId;
        //付款金额，必填
        String total_amount = account;
        //订单名称，必填
        String subject = "酒店订单页";
        //商品描述，可空
        String body = null;

        // 该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。 该参数数值不接受小数点， 如 1.5h，可转换为 90m。
        String timeout_express = "1c";

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout_express + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
       ;
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("dsasdasddsddd");
        //输出
        return result;
    }

//    @RequestMapping(value = "/return_url")
//    public String alipayReturnNotice(HttpServletRequest httpServletRequest, Model model, HttpSession httpSession) throws Exception {
//        httpServletRequest.getSession().getAttribute("orderInformation");
//        OrderFormation orderFormation = (OrderFormation) httpSession.getAttribute("orderInformation");
//        System.out.println(orderFormation.toString());
//        //获取支付宝GET过来反馈信息
//
//        return "orderView.html";
//    }

}
